//
//  AY_LastPageViewController.m
//  AiYue
//
//  Created by mac on 2023/12/26.
//

#import "AY_LastPageViewController.h"
#import "AY_LastPageHeader.h"
#import "AY_LastPageCell.h"

@interface AY_LastPageViewController ()<SXLoadingDelegate>

@property (nonatomic, strong) AY_LastPageHeader *headerView;
@property (strong,nonatomic) AY_LoadingView *loadingView;
@end

@implementation AY_LastPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.frame = CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, SCREEN_W, SCREEN_H - GK_STATUSBAR_NAVBAR_HEIGHT);
    self.tableView.tableHeaderView = self.headerView;
    [self getData];
}

- (void)getData {
    [self.loadingView loadingInView:self.view];
    [AYApiRequest getBookRecommendListWithArticleId:self.bookModel.bookId andLimit:@"10000" Success:^(AY_AudioBookModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            [self.loadingView dismiss];
            self.dataSource = [NSMutableArray arrayWithArray:pkg.data];
            self.headerView.bookModel = self.bookModel;
            [self.tableView reloadData];
        }else {
            [self.loadingView refresh];
        }
    } failure:^(int status, NSString * _Nonnull info) {
        [self.loadingView refresh];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"AY_LastPageCell";
    AY_LastPageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[AY_LastPageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_PageReadViewController *vc = [[AY_PageReadViewController alloc] init];
    BookModel *model = self.dataSource[indexPath.row];
    AY_ReadBookModel *model1 = [AYTool getBookDataWithBookId:model.bookId];
    [vc sendBookId:model.bookId andPage:model1.page andChapterIndex:model1.chapterIndex];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(110);
}

- (AY_LastPageHeader *)headerView {
    if (!_headerView) {
        _headerView = [[AY_LastPageHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, YNUM(220))];
    }
    return _headerView;
}

- (AY_LoadingView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[AY_LoadingView alloc] initWithFrame:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, SCREEN_W, SCREEN_H - GK_STATUSBAR_NAVBAR_HEIGHT)];
        _loadingView.backgroundColor = [UIColor whiteColor];
        _loadingView.delegate = self;
    }
    return _loadingView;
}

- (void)refreshAction {
    [self getData];
}
@end
